#include "mypushbutton.h"
#include "drawing.h"
#include <QLabel>


mypushbutton::mypushbutton(const QString &text, QWidget *parent) : QPushButton(text, parent)
{

    // 创建绘图场景（白板）
    scene = new DrawingScene(parent);
    scene->setSceneRect(0, 0, 800, 600);

    // 创建绘图视图，使场景可视化
    view = new QGraphicsView(scene, parent);
    view->setRenderHint(QPainter::Antialiasing);
    view->setFocusPolicy(Qt::StrongFocus);
    view->setSceneRect(0, 0, 1490, 720);




    // 创建撤销按钮
    QPushButton *undoButton = new QPushButton("撤销", parent);
    undoButton->setFixedSize(80, 30);
    QObject::connect(undoButton, &QPushButton::clicked, scene, &DrawingScene::undo);

    // 创建前进按钮
    QPushButton *redoButton = new QPushButton("前进", parent);
    redoButton->setFixedSize(80, 30);
    QObject::connect(redoButton, &QPushButton::clicked, scene, &DrawingScene::redo);

    // 创建颜色选择按钮
    QPushButton *colorButton = new QPushButton("选择颜色", parent);
    colorButton->setFixedSize(80, 30);
    QObject::connect(colorButton, &QPushButton::clicked, [&]()
    {
        QColorDialog dialog;
        QColor color = dialog.getColor();
        if (color.isValid()) {
            scene->setPenColor(color);
        }
    });

    // 创建画笔宽度按钮和文本
    QPushButton *widthbutton = new QPushButton("画笔宽度",parent);
    widthbutton->setFixedSize(80, 30);
    QLabel *widthLabel = new QLabel("1", parent);
    widthLabel->setFixedWidth(30);
    QObject::connect(widthbutton, &QPushButton::clicked, [=](){
        static int penWidth = 1;
        penWidth++;
    if (penWidth > 15)
    {
        penWidth = 1;
    }
    QString widthStr = QString::number(penWidth);
    widthLabel->setText(widthStr);
    scene->setPenWidth(penWidth);
    });

     // 创建橡皮擦宽度按钮和文本
    QPushButton *widthbutton0 = new QPushButton("橡皮擦宽度",parent);
    widthbutton0->setFixedSize(80, 30);
    QLabel *widthLabel0 = new QLabel("5", parent);
    widthLabel0->setFixedWidth(30);
    QObject::connect(widthbutton0, &QPushButton::clicked, [=](){
     static int m_eraseWidth = 5;
     m_eraseWidth+=5;
    if (m_eraseWidth > 40)
    {
    m_eraseWidth = 5;
    }
     QString widthStr = QString::number(m_eraseWidth);
     widthLabel0->setText(widthStr);
    scene->setEraseWidth(m_eraseWidth);
    });

    //创建保存按钮
    QPushButton *saveButton = new QPushButton("保存图片", parent);
    saveButton ->setFixedSize(80, 30);
    QObject::connect(saveButton, &mypushbutton::clicked, this, &mypushbutton::saveImage);


    // 创建水平布局，将按钮添加到其中
    buttonLayout = new QHBoxLayout();
    buttonLayout->addWidget(undoButton);
    buttonLayout->addWidget(redoButton);
    buttonLayout->addStretch();
    buttonLayout->addWidget(colorButton);
    buttonLayout->addWidget(widthbutton);
    buttonLayout->addWidget(widthLabel);
    buttonLayout->addWidget(widthbutton0);
    buttonLayout->addWidget(widthLabel0);
    buttonLayout->addWidget(saveButton);




    // 创建垂直布局，将绘图视图和水平布局添加到其中
    mainLayout = new QVBoxLayout();
    mainLayout->addWidget(view);
    mainLayout->addLayout(buttonLayout);


}
//保存功能
void mypushbutton::saveImage()
{
    QPixmap pixmap = view->grab();
    QString fileName = QFileDialog::getSaveFileName(this, tr("Save Image"), "", tr("Images (*.png *.bmp *.jpg)"));
    if (!fileName.isEmpty())
    {
        pixmap.save(fileName);
    }
}


